//
//  RMMSkillDeltailsViewController.m
//  Romomo
//
//  Created by puman on 2017/2/7.
//  Copyright © 2017年 mazegong. All rights reserved.
//

#import "RMMSkillDeltailsViewController.h"
#import "RMMSkillHeadView.h"
#import "RMMSkillDetalTableViewCell.h"
#import "RMMOderAdressViewController.h"
#import "RMMDaShangView.h"
#import "RMMPayOrderViewController.h"

@interface RMMSkillDeltailsViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@end

@implementation RMMSkillDeltailsViewController
{
    UITableView *_tableView;
    RMMDaShangView *_daShangView;
    RMMSkillHeadView *_headView;
    NSMutableDictionary *_rowHeightDic;
    NSMutableDictionary *_moneyDic;
    NSArray *_bqArray;
    NSArray *_array;
    NSInteger _buttonTag;
    BOOL _isOpen;
    NSInteger _index;
    NSDictionary *_dic;
}
- (void)dealloc{
    NS_REM(@"ZHANGKAIGENGDUO", nil);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"技师主页";
    self.view.backgroundColor = kCColor;
    [self addViews];
    [self getMySkillData];
    // Do any additional setup after loading the view.
}
- (void)getMySkillData{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"emp_id"] = _emp_id;
    NSString *url = GetOneEmpDetailUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (!isNetwork) {
            [_tableView.mj_header endRefreshing];
        }
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _dic = dataDic[@"data"];
            _headView.dic = _dic;
            _bqArray = _dic[@"LABELS"];
            _array = _dic[@"EVALUATE_DETAILS"][@"EvaluateList"];
            [self checkedRowHeightWith:_array];
            if (_bqArray.count) {
                _headView.array = _bqArray;
            }
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    } error:^(NSError *errors) {
        [_tableView.mj_header endRefreshing];
    } view:self.view];
}

- (void)addViews{
    _headView = [[RMMSkillHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 242)];
    _isOpen = NO;
    NS_ADD(moreBut:, @"ZHANGKAIGENGDUO", nil);
    _rowHeightDic = [NSMutableDictionary dictionary];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64 - 49) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableHeaderView = _headView;
    [_tableView registerClass:[RMMSkillDetalTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 0)];
    
    if (![_type isEqualToString:@"1"]) {
        [self addBottomView];
    }
    _daShangView = [[RMMDaShangView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWideth, kScreenHeight)];
    _daShangView.textF.delegate =  self;
    TEXTFIELD_S(_daShangView.textF, editorMoney:);
    BUTTON_S(_daShangView.button, payMoneyButClick);
    _moneyDic = [NSMutableDictionary dictionary];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [_daShangView viewWithTag:50 + i];
        BUTTON_S(button, chooseMoneyClick:);
        [_moneyDic setObject:@"0" forKey:[NSString stringWithFormat:@"%ld",button.tag]];
    }
    [keyWidow addSubview:_daShangView];
}
- (void)moreBut:(NSNotification *)notification{
    NSDictionary *dic = notification.userInfo;
    _index = [dic[@"index"] integerValue];
    UIButton *button = [_headView viewWithTag:1000 + _index];
    BUTTON_S(button, moreButClick:);
}
- (void)moreButClick:(UIButton *)button{
    _isOpen = !_isOpen;
    if (_isOpen) {
        _headView.frame = CGRectMake(0, 0, kScreenWideth, 242 + 35 * _headView.index);
        [button setTitle:@"收起" forState:(UIControlStateNormal)];
        [button setImage:[UIImage imageNamed:@"moredown@3x"] forState:(UIControlStateNormal)];
        button.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        for (int i = 0; i <= _bqArray.count; i++) {
            UIButton *but = [_headView viewWithTag:1000 + i];
            but.hidden = NO;
        }
    } else {
        _headView.frame = CGRectMake(0, 0, kScreenWideth, 242);
        [button setTitle:@"展开查看更多" forState:(UIControlStateNormal)];
        button.imageView.transform = CGAffineTransformMakeRotation(M_PI * 2);
        [button setImage:[UIImage imageNamed:@"moredown@3x"] forState:(UIControlStateNormal)];
        for (int i = 0; i <= _bqArray.count; i++) {
            UIButton *but = [_headView viewWithTag:1000 + i];
            but.hidden = YES;
            if (i <= _index) {
                but.hidden = NO;
            }
        }
    }
    _tableView.tableHeaderView = _headView;
    [_tableView reloadData];
}
- (void)addBottomView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 49 - 64, kScreenWideth, 49)];
    view.backgroundColor = kWhiteColor;
    [self.view addSubview:view];
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, view.width, 0.5)];
    lineLabel.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
    [view addSubview:lineLabel];
    
    CGFloat width = (kScreenWideth - 30 - 10) / 2.0;
    for (int i = 0; i < 2; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(15 + (width + 10) * i , 6, width, 36);
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 18.0;
        button.titleLabel.font = kFont(15);
        if (i == 0) {
            [button setTitle:@"打赏" forState:(UIControlStateNormal)];
            [button setTitleColor:kBasicColor forState:(UIControlStateNormal)];
            button.layer.borderWidth = 1;
            button.layer.borderColor = kBasicColor.CGColor;
            BUTTON_S(button, daShangButClick);
        } else {
            [button setTitle:@"预约" forState:(UIControlStateNormal)];
            [button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
            button.backgroundColor = kBasicColor;
            BUTTON_S(button, bookingButClick);
        }
        [view addSubview:button];
    }
}
- (void)editorMoney:(UITextField *)textF{
    if (_daShangView.textF.text.length) {
        [self buttonOn];
    } else {
        NSMutableArray *array =  [NSMutableArray array];
        for (NSString *key in _moneyDic) {
            [array addObject:_moneyDic[key]];
        }
        if ([array containsObject:@"1"]) {
            [self buttonOn];
        } else {
            [self buttonOff];
        }
    }
}
- (void)bookingButClick{
    RMMOderAdressViewController *VC = [[RMMOderAdressViewController alloc] init];
    [RMMFileManager shareHandle].fromSkillType = @"1";
    VC.type = @"1";
    VC.isHome = [RMMFileManager shareHandle].fuWuType;
    NSDictionary *dic = @{@"EMP_NAME":_dic[@"EMPLOYEE_NAME"],@"EMP_ID":_dic[@"EMP_ID"]};
    [[RMMFileManager shareHandle].bookingDic setValue:dic forKey:@"技师"];
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)daShangButClick{
    [UIView animateWithDuration:kInterval animations:^{
        _daShangView.frame = CGRectMake(0, 0, kScreenWideth, kScreenHeight);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)buttonOn{
    [_daShangView.button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
    _daShangView.button.backgroundColor = kBasicColor;
    _daShangView.button.userInteractionEnabled = YES;
}
- (void)buttonOff{
    [_daShangView.button setTitleColor:COLORFROMCODE(0xB5B5B5, 1) forState:(UIControlStateNormal)];
    _daShangView.button.backgroundColor = kWhiteColor;
    _daShangView.button.userInteractionEnabled = NO;
}
- (void)chooseMoneyClick:(UIButton *)button{
    _buttonTag = button.tag;
    NSString *status = [_moneyDic objectForKey:[NSString stringWithFormat:@"%ld",_buttonTag]];
    for (int i = 0; i < 3; i++) {
        UIButton *buttons = [_daShangView viewWithTag:50 + i];
        if (buttons.tag == _buttonTag) {
            if ([status isEqualToString:@"0"]) {
                buttons.backgroundColor = kBasicColor;
                [buttons setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
                [_moneyDic setObject:@"1" forKey:[NSString stringWithFormat:@"%ld",_buttonTag]];
                if (!_daShangView.textF.text.length) {
                    [self buttonOn];
                }
            } else {
                buttons.backgroundColor = kWhiteColor;
                [buttons setTitleColor:kBasicColor forState:(UIControlStateNormal)];
                [_moneyDic setObject:@"0" forKey:[NSString stringWithFormat:@"%ld",_buttonTag]];
                if (!_daShangView.textF.text.length) {
                    [self buttonOff];
                }
            }
        } else {
            buttons.backgroundColor = kWhiteColor;
            [buttons setTitleColor:kBasicColor forState:(UIControlStateNormal)];
            [_moneyDic setObject:@"0" forKey:[NSString stringWithFormat:@"%ld",buttons.tag]];
        }
    }
}
- (void)payMoneyButClick{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"pay_code"] = [RMMFileManager shareHandle].fuWuType;
    parDic[@"cust_id"] = [RMMFileManager shareHandle].userDic[@"CUST_ID"];
    parDic[@"emp_id"] = _dic[@"EMP_ID"];
    parDic[@"order_source"] = @"1";
    if (_daShangView.textF.text.length && [_daShangView.textF.text integerValue] > 0) {
        parDic[@"atm"] = _daShangView.textF.text;
    } else {
        UIButton *button = [_daShangView viewWithTag:_buttonTag];
        NSString *title = [button titleForState:(UIControlStateNormal)];
        parDic[@"atm"] = [title substringToIndex:title.length - 1];
    }
    NSString *url = DaShangSkillUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [UIView animateWithDuration:kInterval animations:^{
                _daShangView.frame = CGRectMake(0, kScreenHeight, kScreenWideth, kScreenHeight);
            } completion:^(BOOL finished) {
                RMMPayOrderViewController *VC = [[RMMPayOrderViewController alloc] init];
                VC.order_number = dataDic[@"data"];
                VC.type = @"打赏";
                VC.totalPrice = parDic[@"atm"];
                if (_daShangView.textF.text.length && [_daShangView.textF.text integerValue] > 0) {
                    VC.totalPrice = _daShangView.textF.text;
                } else {
                    UIButton *button = [_daShangView viewWithTag:_buttonTag];
                    NSString *title = [button titleForState:(UIControlStateNormal)];
                    VC.totalPrice = [title substringToIndex:title.length - 1];
                }
                [self.navigationController pushViewController:VC animated:YES];
            }];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];

    } error:^(NSError *errors) {
        
    } view:self.view];
}

- (void)checkedRowHeightWith:(NSArray *)array{
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = array[i];
        NSString *content = dic[@"REMARK"];
        CGFloat height = [UILabel backHeightWithString:content CGFloatCount:kScreenWideth - 30 metho:HEIGHT font:14];
        [_rowHeightDic setObject:[NSString stringWithFormat:@"%f",28 + height + 8] forKey:[NSString stringWithFormat:@"%d",i]];
    }
}
- (void)daShangViewHide{
    [UIView animateWithDuration:kInterval animations:^{
        _daShangView.frame = CGRectMake(0, kScreenHeight, kScreenWideth, kScreenHeight);
    } completion:^(BOOL finished) {
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *key = [NSString stringWithFormat:@"%ld",indexPath.row];
    return [[_rowHeightDic objectForKey:key] floatValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RMMSkillDetalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dic = _array[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
